<?php
/**
 * Feed manager tests
 *
 * PHP version 5
 *
 * @package    TV
 * @subpackage Tests
 * @author     Mike Bevz <mike.bevz@data2people.com>
 * @copyright  2007-2008 (c) Mike Bevz
 * @license    The BSD License http://data2people.com/license.txt
 * @version    SVN: $Id: CloudTests.php 13 2008-12-01 16:16:50Z michael.bevz $
 *
 */

require_once dirname(__FILE__).'/../testsConfig.php';
require_once 'PHPUnit/Framework.php';

class FeedsTests extends PHPUnit_Framework_TestCase
{
    /**
     * Model class
     *
     * @var TV_Manager_Feeds
     */
    private $_manager;
    
    
    protected function setUp()
    {
        $this->_manager = new TV_Manager_Feeds();
        $this->_manager->setConfig(dirname(__FILE__).DIRECTORY_SEPARATOR
                                  .'data'.DIRECTORY_SEPARATOR.'feeds.xml');
    }

    protected function tearDown()
    {
      unset($this->_manager);
    }
    
    public function testGetFeedList()
    {
        $expected = array('url1', 'url2', 'url3', 'url4', 'url5', 'url6', 'url7');
        $result = $this->_manager->getFeedList();
        $this->assertEquals($expected, $result);
    }
    
    /**
     * @expectedException TV_Manager_Exception
     */
    public function testUpdateFeedAcceptsOnlyString()
    {
        $this->_manager->updateFeed(array());
    }
    
    /**
     * @expectedException Zend_Uri_Exception
     *
     */
    public function testUpdateFeedReturnsErrorOnNonExistentScheme()
    {
        $this->_manager->updateFeed('nonexistent-scheme');
    }
    
    /**
     * @expectedException TV_Manager_Exception
     *
     */
    public function testUpdateFeedReturnsErrorOnNonExistentFeed()
    {
        $this->_manager->updateFeed('http://example.com/somefeed.xml');
    }
    
    public function te__stUpdateFeed()
    {
        $link = 'http://rss.news.yahoo.com/rss/topstories';
        $expectedLink = 'http://news.yahoo.com/i/716';
        $this->_manager->updateFeed($link);
        
        $feed = new Feed();
        $results = $feed->getByLink($expectedLink);
        if (count($results) == 0) {
            $this->fail(__METHOD__.': No results saved into database!');
        } else {
            $this->assertEquals($expectedLink, $results[0]->link);
            $results[0]->delete();
        }
                
    }
    
    public function testCreateCloud()
    {
        $link = 'http://rss.news.yahoo.com/rss/topstories';
        $expectedLink = 'http://news.yahoo.com/i/716';
        $this->_manager->updateFeed($link);
                
        $this->_manager->createCloud();
        
        $feed = new Feed();
        $results = $feed->getByLink($expectedLink);
        if (count($results) == 0) {
            $this->fail(__METHOD__.': No results saved into database!');
        } else {
            $this->assertEquals($expectedLink, $results[0]->link);
            $results[0]->delete();
        }
        
    }
}